home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / UsingPDF / GhostScript / data / pdf_sec.ps < prev    next >
Encoding:
Text File  |  1999-09-22  |  10.9 KB  |  387 lines

  1. %    Copyright (C) 1996-1998 Geoffrey Keating. 
  2. % This file may be freely distributed with or without modifications,
  3. % so long as modified versions are marked as such and copyright notices are
  4. % not removed.
  5.  
  6. % pdf_sec.ps (version 1.0.4)
  7. % Implementation of security hooks for PDF reader.
  8.  
  9. % This file contains the procedures that have to take encryption into
  10. % account when reading a PDF file. It replaces the stub version of this
  11. % file that is shipped with GhostScript. It requires GhostScript version 4.02
  12. % or later.
  13.  
  14. % Documentation for using this file is available at
  15. % http://www.ozemail.com.au/%7Egeoffk/pdfencrypt/
  16.  
  17. /.setlanguagelevel where { pop 2 .setlanguagelevel } if
  18. .currentglobal true .setglobal
  19. /pdfdict where { pop } { /pdfdict 100 dict def } ifelse
  20. pdfdict begin
  21.  
  22. % Older ghostscript versions do not have .pdftoken, so we use 'token' instead.
  23. /.pdftoken where { pop } { /.pdftoken /token load def } ifelse
  24.  
  25. % An implementation of an algorithm compatible with the RSA Data Security 
  26. % Inc. RC4 stream encryption algorithm.
  27.  
  28. % <string> rc4setkey <dict>
  29. /rc4setkey
  30. {
  31.   6 dict begin
  32.     /k exch def
  33.     /a 256 string def
  34.     0 1 255 { a exch dup put } for
  35.     /l k length def
  36.     /j 0 def
  37.     0 1 255
  38.     {
  39.       /i exch def
  40.       /j a i get k i l mod get add j add 255 and def
  41.       a i a j get a j a i get put put
  42.     } for
  43.     3 dict dup begin
  44.       /a a def
  45.       /x 0 def
  46.       /y 0 def
  47.     end
  48.   end
  49. } bind def
  50.  
  51. % <rc4key> <string> rc4 <string> <rc4key>
  52. /rc4
  53. {
  54.   1 index begin
  55.     dup dup length 1 sub 0 exch 1 exch
  56.     {
  57.       /x x 1 add 255 and def
  58.       /y a x get y add 255 and def
  59.       a x a y get a y a x get put put
  60. % stack: string string index
  61.       2 copy get
  62.       a dup x get a y get add 255 and get
  63.       xor put dup
  64.     } for
  65.     pop
  66.   end
  67. } bind def
  68.  
  69. % take a stream and rc4 decrypt it.
  70. % <stream> <key> rc4decodefilter <stream>
  71. /rc4decodefilter {
  72.   currentglobal exch true setglobal
  73.   dup length string copy rc4setkey
  74.   exch setglobal
  75.   exch 512 string
  76.    % stack: <key> <stream> <string>
  77.   { readstring pop rc4 exch pop } aload pop
  78.    8 array astore cvx 0 () /SubFileDecode filter 
  79. } bind def
  80.  
  81. % MD5 derived from RFC 1321, "The MD5 Message-Digest Algorithm",
  82. % R. Rivest, MIT, RSADSI; implemented in PostScript by Geoffrey Keating.
  83.  
  84. % We construct the MD5 transform by a sort of inline expansion.
  85. % this takes up quite a bit of memory (around 17k), but gives a
  86. % factor-of-two speed increase.
  87. % This also allows us to take advantage of interpreters with 64-bit
  88. % wide integers.
  89. % This will not run on interpreters with 16-bit wide integers, if such
  90. % things exist.
  91. 20 dict begin
  92.  
  93. /T [
  94. 16#d76aa478 16#e8c7b756 16#242070db 16#c1bdceee
  95. 16#f57c0faf 16#4787c62a 16#a8304613 16#fd469501
  96. 16#698098d8 16#8b44f7af 16#ffff5bb1 16#895cd7be
  97. 16#6b901122 16#fd987193 16#a679438e 16#49b40821
  98. 16#f61e2562 16#c040b340 16#265e5a51 16#e9b6c7aa
  99. 16#d62f105d 16#02441453 16#d8a1e681 16#e7d3fbc8
  100. 16#21e1cde6 16#c33707d6 16#f4d50d87 16#455a14ed
  101. 16#a9e3e905 16#fcefa3f8 16#676f02d9 16#8d2a4c8a
  102. 16#fffa3942 16#8771f681 16#6d9d6122 16#fde5380c
  103. 16#a4beea44 16#4bdecfa9 16#f6bb4b60 16#bebfbc70
  104. 16#289b7ec6 16#eaa127fa 16#d4ef3085 16#04881d05
  105. 16#d9d4d039 16#e6db99e5 16#1fa27cf8 16#c4ac5665
  106. 16#f4292244 16#432aff97 16#ab9423a7 16#fc93a039
  107. 16#655b59c3 16#8f0ccc92 16#ffeff47d 16#85845dd1
  108. 16#6fa87e4f 16#fe2ce6e0 16#a3014314 16#4e0811a1
  109. 16#f7537e82 16#bd3af235 16#2ad7d2bb 16#eb86d391
  110. ] def
  111. /F [
  112. { c d /xor b /and d /xor } { b c /xor d /and c /xor }
  113. { b c /xor d /xor } { d /not b /or c /xor }
  114. ] def
  115. /R [
  116. 16#0007 16#010c 16#0211 16#0316 16#0407 16#050c 16#0611 16#0716
  117. 16#0807 16#090c 16#0a11 16#0b16 16#0c07 16#0d0c 16#0e11 16#0f16
  118. 16#0105 16#0609 16#0b0e 16#0014 16#0505 16#0a09 16#0f0e 16#0414
  119. 16#0905 16#0e09 16#030e 16#0814 16#0d05 16#0209 16#070e 16#0c14
  120. 16#0504 16#080b 16#0b10 16#0e17 16#0104 16#040b 16#0710 16#0a17
  121. 16#0d04 16#000b 16#0310 16#0617 16#0904 16#0c0b 16#0f10 16#0217
  122. 16#0006 16#070a 16#0e0f 16#0515 16#0c06 16#030a 16#0a0f 16#0115
  123. 16#0806 16#0f0a 16#060f 16#0d15 16#0406 16#0b0a 16#020f 16#0915
  124. ] def
  125.  
  126. /W 1 31 bitshift 0 gt def
  127. /A W { /add } { /md5add } ifelse def
  128. /t W { 1744 } { 1616 } ifelse array def
  129. /C 0 def
  130.  
  131. 0 1 63 {
  132.   /i exch def
  133.   /r R i get def
  134.   /a/b/c/d 4 i 3 and roll [ /d/c/b/a ] { exch def } forall
  135.  
  136.   t C [
  137.     a F i -4 bitshift get exec 
  138.     a A /x r -8 bitshift /get A T i get A
  139.     W { 1 32 bitshift 1 sub /and } if
  140.     /dup r 31 and /bitshift /exch r 31 and 32 sub /bitshift /or
  141.     b A
  142.     /def
  143.   ] dup length C add /C exch def putinterval
  144. } for
  145.  
  146. 1 1 C 1 sub {
  147.   dup 1 sub t exch get /def cvx eq
  148.     {pop}
  149.     {t exch 2 copy get cvx put}
  150.   ifelse
  151. } for
  152.  
  153. % If we could put t into a _packed_ array, its memory requirements
  154. % would go from about 13k to about 4k. Unfortunately, we'd need around
  155. % 1600 stack positions, around 3 times what we can expect to have 
  156. % available---and if that kind of memory is available, we don't really
  157. % need to pack t. Sigh.
  158.  
  159. % In fact, it's worse than that. You can't even determine what t will 
  160. % be and write it in directly (something like
  161. % { /a c d xor b and d xor a md5add x 0 get md5add -680876936 md5add dup 7 
  162. %   bitshift exch -25 bitshift or b md5add def /d b c xor a ...
  163. % ) because the scanner uses the operand stack to accumulate procedures.
  164. % So the only way to have md5transform as a single procedure is the above 
  165. % trick.
  166.  
  167. W /md5transform t end cvx bind def
  168.  
  169. % Unfortunately, PostScript & its imitators convert large
  170. % integers to floating-point. Worse, the fp representation probably
  171. % won't have 32 significant bits.
  172. % This procedure accounts for about 35% of the total time on 32-bit 
  173. % machines.
  174. not {
  175.   /md5add {
  176.     2 copy xor 0 lt
  177.       % if one is positive and one is negative, can't overflow
  178.       { add }
  179.       % if both are positive or negative
  180.       { 16#80000000 xor add 16#80000000 xor }
  181.       % same as subtracting (or adding) 2^31 and then subtracting (or 
  182.       % adding) it back.
  183.     ifelse
  184.   } bind def
  185. } {
  186.   /md5add {
  187.     add 16#0FFFFFFFF and
  188.   } bind def
  189. } ifelse
  190.  
  191. /md5 {
  192.   20 dict begin
  193.  
  194.   % initialise a,b,c,d,x
  195.   /a 16#67452301 def
  196.   /b 16#efcdab89 def
  197.   /c 16#98badcfe def
  198.   /d 16#10325476 def
  199.   /x 16 array def
  200.  
  201.   % parameters
  202.   /origs exch def
  203.   /oslen origs length def
  204.  
  205.   % pad string to multiple of 512 bits
  206.   /s oslen 72 add 64 idiv 64 mul dup /slen exch def string def
  207.   s 0 origs putinterval
  208.   s oslen 16#80 put
  209.   s slen 8 sub oslen 31 and 3 bitshift put
  210.   s slen 7 sub oslen -5 bitshift 255 and put
  211.   s slen 6 sub oslen -13 bitshift 255 and put
  212.  
  213.   0 64 slen 64 sub {
  214.     dup 1 exch 63 add { s exch get } for
  215.     15 -1 0 { x exch 6 2 roll 3 { 8 bitshift or } repeat put } for
  216.     a b c d
  217.     md5transform
  218.     d md5add /d exch def
  219.     c md5add /c exch def
  220.     b md5add /b exch def
  221.     a md5add /a exch def
  222.   } for
  223.   
  224.   16 string
  225.   [ [ a b c d ] { 3 { dup -8 bitshift } repeat } forall ]
  226.   0 1 15 {
  227.     3 copy dup 3 1 roll get 255 and put pop
  228.   } for
  229.   pop
  230.  
  231.   end
  232. } bind def
  233.  
  234. % Pad a key out to 32 bytes.
  235. /pdf_pad_key        % <key> pdf_pad_key <padded key>
  236.  { dup length 32 gt { 0 32 getinterval } if
  237.    <28bf4e5e4e758a41 64004e56fffa0108
  238.     2e2e00b6d0683e80 2f0ca9fe6453697a>
  239.    0 32 3 index length sub getinterval
  240.    concatstrings
  241.  } bind def
  242.  
  243. % Try a user key for a PDF file.
  244. /pdf_try_key        % <key> pdf_try_key <filekey> true
  245.             % <key> pdf_try_key false
  246.  { Trailer /Encrypt oget
  247.    dup /O oget exch
  248.    /P oget 4 string exch
  249.      2 copy 255 and 0 exch put
  250.      2 copy -8 bitshift 255 and 1 exch put
  251.      2 copy -16 bitshift 255 and 2 exch put
  252.      2 copy -24 bitshift 255 and 3 exch put pop
  253.    Trailer /ID oget 0 oget
  254.    3 { concatstrings } repeat
  255.    md5 0 5 getinterval
  256.    dup
  257.    Trailer /Encrypt oget /U oget dup length string copy exch
  258.    rc4setkey exch rc4 exch pop
  259.    <28bf4e5e4e758a41 64004e56fffa0108
  260.     2e2e00b6d0683e80 2f0ca9fe6453697a> eq
  261.    dup not { exch pop } if
  262.  } bind def
  263.  
  264. % Process the encryption information in the Trailer.
  265. /pdf_process_Encrypt
  266.  { Trailer /Encrypt oget
  267.    /Filter oget /Standard eq not
  268.     { (****This file uses an unknown security handler.\n) print flush
  269.     /pdf_process_Encrypt cvx /undefined signalerror
  270.     }
  271.    if
  272.    () pdf_pad_key pdf_try_key
  273.     { /FileKey exch def } 
  274.     { /PDFPassword where
  275.        { pop PDFPassword pdf_pad_key pdf_try_key 
  276.       { true }
  277.       { PDFPassword pdf_pad_key md5 0 5 getinterval rc4setkey
  278.         Trailer /Encrypt oget /O oget dup length string copy
  279.            rc4 exch pop
  280.         pdf_try_key
  281.       }
  282.      ifelse
  283.       { /FileKey exch def }
  284.       { (****Password did not work.\n) print flush
  285.         /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  286.       }
  287.      ifelse 
  288.        }
  289.        { (****This file has a user password set.\n) print flush
  290.      /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  291.        }
  292.       ifelse
  293.     }
  294.    ifelse
  295.    Trailer /Encrypt oget /P oget 4 and 0 eq #? and
  296.     { (****This owner of this file has requested you do not print it.\n)
  297.     print flush
  298.       /pdf_process_Encrypt cvx /invalidfileaccess signalerror
  299.     }
  300.    if
  301.  } bind def
  302.  
  303. % Calculate the key used to decrypt an object (to pass to .decpdfrun or
  304. % put into a stream dictionary).
  305. /computeobjkey    % <object#> <generation#> computeobjkey <keystring>
  306. {
  307.   exch
  308.   10 string
  309.   dup 0 FileKey putinterval
  310.   exch
  311.         % stack:  gen# string obj#
  312.     2 copy 255 and 5 exch put
  313.     2 copy -8 bitshift 255 and 6 exch put
  314.     2 copy -16 bitshift 255 and 7 exch put
  315.   pop exch
  316.     2 copy 255 and 8 exch put
  317.     2 copy -8 bitshift 255 and 9 exch put
  318.   pop md5 0 10 getinterval
  319. } bind def
  320.  
  321. % As .pdfrun, but decrypt strings with key <key>.
  322. /.decpdfrun            % <file> <keystring> <opdict> .decpdfrun -
  323.  {    % Construct a procedure with the file, opdict and key bound into it.
  324.    2 index cvlit mark mark 5 2 roll
  325.     { .pdftoken not { (%%EOF) cvn cvx } if
  326.       dup xcheck
  327.        { DEBUG { dup == flush } if
  328.      3 -1 roll pop
  329.      2 copy .knownget
  330.       { exch pop exch pop exec }
  331.       { (%stderr) (w) file
  332.         dup (****************Unknown operator: ) writestring
  333.         dup 3 -1 roll .writecvs dup (\n) writestring flushfile
  334.         pop
  335.       }
  336.      ifelse
  337.        }
  338.        { exch pop DEBUG { dup ==only ( ) print flush } if
  339.      dup type /stringtype eq
  340.       { exch rc4setkey exch rc4 }
  341.      if
  342.      exch pop
  343.        }
  344.       ifelse
  345.     }
  346.    aload pop .packtomark cvx
  347.    /loop cvx 2 packedarray cvx
  348.     { stopped /PDFsource } aload pop
  349.    PDFsource
  350.     { store { stop } if } aload pop .packtomark cvx 
  351.    /PDFsource 3 -1 roll store exec
  352.  } bind def
  353.  
  354. % Run the code to resolve an object reference.
  355. /pdf_run_resolve
  356.  { /FileKey where
  357.     { pop
  358.       2 copy computeobjkey dup 4 1 roll
  359.       PDFfile exch resolveopdict .decpdfrun
  360.       dup dup dup 5 2 roll
  361.     % stack: object object key object object
  362.       xcheck exch type /dicttype eq and
  363.        { /StreamKey exch put }
  364.        { pop pop }
  365.       ifelse
  366.     }
  367.     { PDFfile resolveopdict .pdfrun }
  368.    ifelse
  369.  } bind def
  370.  
  371. % Prefix a decryption filter to a stream if needed.
  372. % Stack: readdata? dict parms file/string filternames
  373. % (both before and after).
  374. /pdf_decrypt_stream
  375.  { 3 index /StreamKey known
  376.    {
  377.       exch 
  378.     % Stack: readdata? dict parms filternames file/string
  379.       3 index /Length oget () /SubFileDecode filter
  380.       3 index /StreamKey get rc4decodefilter
  381.       exch
  382.    } if
  383.  } bind def
  384.  
  385. end            % pdfdict
  386. .setglobal
  387.